﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using GraphGeneric;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace GraphGenericTestApp
{
	public class GraphNodeCanvasOld : Canvas
	{
		public GraphNodeCanvasOld(INodeOld<string, string> node, GraphCanvasOld canvas)
		{
			Opacity = 1;
			m_node = node;
			m_parentCanvas = canvas;

			// TODO : this code needs to be replace with more dynamic ways of handling size,
			//            shapes, colors, etc..
			MinHeight = 50;
			MinWidth = 100;
			Width = MinWidth;
			Height = MinHeight;
			m_shape = new Ellipse();
			m_shape.Fill = Brushes.Orange;
			m_shape.Stroke = Brushes.Tomato;
			m_shape.StrokeThickness = 1;

			m_textBlock = new TextBlock();
			m_textBlock.Text = m_node.Value;
			m_textBlock.FontSize = 16;
			m_textBlock.FontFamily = new FontFamily("Ariel");
			Children.Add(m_shape);
			Children.Add(m_textBlock);
		}

		#region Dependency Properties

		public Point Location
		{
			get { return (Point)GetValue(LocationProperty); }
			set { SetValue(LocationProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Location.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty LocationProperty =
			 DependencyProperty.Register("Location", typeof(Point), typeof(GraphNodeCanvasOld), new UIPropertyMetadata(new Point(0, 0), new PropertyChangedCallback(OnLocationChanged)));

		private static void OnLocationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			GraphNodeCanvasOld node = sender as GraphNodeCanvasOld;
			Canvas.SetLeft(node, node.Location.X - node.Width / 2);
			Canvas.SetTop(node, node.Location.Y - node.Height / 2);
		}

		#endregion

		#region Properties

		public INodeOld<string, string> Node
		{
			get { return m_node; }

		}

		#endregion

		#region Public Methods

		public Rect GetBoundingBox()
		{
			return new Rect(Canvas.GetLeft(this), Canvas.GetTop(this), ActualWidth, ActualHeight);
		}

		#endregion

		#region Event Handlers

		protected override void OnInitialized(EventArgs e)
		{
			m_textBlock.SizeChanged += new SizeChangedEventHandler(m_textBlock_SizeChanged);
			base.OnInitialized(e);
		}
		private void m_textBlock_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			Width = e.NewSize.Width + 30;
			Height = e.NewSize.Height + 10;
			m_shape.Width = Width < MinWidth ? MinWidth : Width;
			m_shape.Height = Height < MinHeight ? MinHeight : Height;
			Canvas.SetLeft(m_textBlock, m_shape.Width / 2 - e.NewSize.Width / 2);
			Canvas.SetTop(m_textBlock, m_shape.Height / 2 - e.NewSize.Height / 2);
		}

		#endregion

		#region Fields

		private INodeOld<string, string> m_node;
		private Canvas m_parentCanvas;
		private TextBlock m_textBlock;
		private Ellipse m_shape;

		#endregion
	}
}
